

#import "ScrollImgView.h"
#define kRandomColor arc4random()%256/255.0

@implementation ScrollImgView

- (instancetype)initWithFrame:(CGRect)frame imageCount:(NSInteger)numOfImgs{
    
    if (self = [super initWithFrame:frame]) {
        _imgCount = numOfImgs;
        [self p_setupView];
    }
    
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor yellowColor];
        _imgCount = 6;
        [self p_setupView];
    }
    return self;
}


//设置视图
- (void)p_setupView{
    
    //初始化最外层scrollView
    self.scroll = [[UIScrollView alloc]initWithFrame:self.bounds];
    self.scroll.backgroundColor = [UIColor blackColor];
    self.scroll.contentSize = CGSizeMake(CGRectGetWidth(self.frame)*_imgCount, CGRectGetHeight(self.frame));
    //设置翻页滚动
    self.scroll.pagingEnabled = YES;
    
    //隐藏横向滚动条
    self.scroll.showsHorizontalScrollIndicator = NO;
    //循环加载图片imageView
    for (int i = 0; i < _imgCount; i++) {
        
        //每张图片imageView
        UIImageView *imV = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.frame)*i, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
        imV.tag = 100+i;
        
        //图片显示模式
        imV.contentMode = UIViewContentModeRedraw;
        
        imV.backgroundColor = [UIColor colorWithRed:kRandomColor green:kRandomColor blue:kRandomColor alpha:1.0];
        
        [self.scroll addSubview:imV];
   
        
    }
    self.scroll.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);

    //初始化UIPageControl 放在屏幕底部
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.bounds), CGRectGetMaxY(self.bounds)-20, CGRectGetWidth(self.bounds), 20)];
    self.page.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    
    //设置小圆点个数
    self.page.numberOfPages = _imgCount-2;
    [self addSubview:self.scroll];
    [self addSubview:self.page];
    
}



@end
